package com.example.avmanagerweb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.avmanagerweb.Utils.String.StringUtils;
import com.example.avmanagerweb.entity.SysDepartment;
import com.example.avmanagerweb.entity.SysUserinfo;
import com.example.avmanagerweb.entity.Userinfo;
import com.example.avmanagerweb.entity.vo.SysUserinfoVo;
import com.example.avmanagerweb.redisService.RedisService;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.SysDepartmentService;
import com.example.avmanagerweb.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysDepartmentService sysDepartmentService;
    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/getUser")
    public RLIST getUser(@RequestBody SysUserinfoVo sysUserinfoVo, HttpServletRequest request){
        //查询自己部门
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }

        //校验
        if(sysUserinfoVo.getPageNo() <= 0){
            //当前页
            return RLIST.error().message("页数异常");
        }
        if(sysUserinfoVo.getPageSize() <= 0){
            //每页条数
            return RLIST.error().message("页条数异常");
        }



        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);
        Integer id = sysUserinfo.getUdepid();
        if(id== null ||id <= 0){
            return RLIST.error().message("异常 账号无部门id");
        }
        List<SysDepartment> departmentList = sysDepartmentService.getSubquery(id);


        Page<Userinfo> page = new Page<>(sysUserinfoVo.getPageNo(), sysUserinfoVo.getPageSize());
        QueryWrapper wrapper = new QueryWrapper();
        Integer uid = sysUserinfoVo.getUid();
        Integer udepid = sysUserinfoVo.getUdepid();
        String uloginname = sysUserinfoVo.getUloginname();
        String uname = sysUserinfoVo.getUname();
        if(uid != null && uid != 0){
            wrapper.eq("uId",uid);
        }
        if(udepid != null && udepid != 0){
            boolean fla = true;
            for (SysDepartment sysDepartment : departmentList) {
                if(sysDepartment.getDepid() == udepid){
                    fla = false;
                    break;
                }
            }
            if(fla){
                return RLIST.error().message("无法跨部门查询");
            }
            wrapper.eq("uDepId",udepid);
        }else {

            List<Integer> depIdList = new ArrayList<>();
            if(departmentList == null || departmentList.size()<=0 ){
                return RLIST.error().message("查询子部门结构异常");
            }
            for (SysDepartment sysDepartment : departmentList) {
                depIdList.add(sysDepartment.getDepid());
            }
            wrapper.in("uDepId", depIdList);
        }
        if(!StringUtils.isEmpty(uloginname)){
            wrapper.like("uLoginName",uloginname);
        }
        if(!StringUtils.isEmpty(uname)){
            wrapper.like("uName",uname);
        }

        wrapper.orderByDesc("uId");

        Page pageList = userinfoService.page(page, wrapper);

        //总条数
        long total = pageList.getTotal();
        List<SysUserinfo> list = pageList.getRecords();
        //页码总数
        long numPage = 0;
        if (total == 0) {
            numPage = 1L;
        } else if (list.size() == 0) {
            numPage = 1L;
        } else {
            numPage = (long) Math.ceil(total / (double) sysUserinfoVo.getPageSize());
        }
        return RLIST.ok().data("data", list).data("total", total).data("numPage", numPage);
    }

}

